/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import DTO.CFields;
import DTO.CUser_group;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CUserGroupDAO extends IData<CUser_group>{
    public CUserGroupDAO(){}

    @Override
    protected Object OnSubmit(CUser_group item, int t, SubConfig config) {
        Object result = 0;
        try {        
            String mysql ="INSERT INTO user_group(ten_group)" + " VALUES (?);";
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            if(t == 0){
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            }
            cs.setString(1, item.getTenGroup());
            int k = cs.executeUpdate();
            if(k == 1 && t == 0){
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(CUserGroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    protected CUser_group getItem(ResultSet rs) {
        CUser_group item  = new CUser_group();
        try {
            
            item.setTenGroup(rs.getString(CFields.ten_group));
            item.setId(rs.getLong(CFields.id));
        } catch (SQLException ex) {
            Logger.getLogger(CUserGroupDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<CUser_group> getAll(SubConfig config) {
        String mysql = "select * from " + CFields.tb_user_group;
       return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CUser_group item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object updateOnSubmint(CUser_group item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object deleteOnSubmint(CUser_group item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   
    @Override
    public CUser_group Find(CUser_group item, List<CUser_group> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CUser_group add(CUser_group item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<CUser_group> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
